package com.yunchao.bank.main.data.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.yunchao.bank.main.data.model.DispatchTaskEntity

@Dao
interface DispatchTaskDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(task: DispatchTaskEntity)

    @Update
    suspend fun update(task: DispatchTaskEntity)

    @Query("SELECT * FROM dispatch_tasks WHERE task_id = :taskId")
    suspend fun getByTaskId(taskId: Int): DispatchTaskEntity?

    @Query("SELECT * FROM dispatch_tasks WHERE is_offline = 1 ORDER BY timestamp DESC")
    suspend fun getAllOfflineTasks(): List<DispatchTaskEntity>

    @Query("SELECT * FROM dispatch_tasks WHERE status = 0 ORDER BY timestamp ASC LIMIT 1")
    suspend fun getNextPendingTask(): DispatchTaskEntity?

    @Query("SELECT * FROM dispatch_tasks WHERE status = 0 AND task_id = :taskId ORDER BY timestamp ASC LIMIT 1")
    suspend fun getNextPendingTask(taskId: Int): DispatchTaskEntity?

    @Query("DELETE FROM dispatch_tasks WHERE task_id = :taskId")
    suspend fun deleteByTaskId(taskId: Int)
}